<?php
function hextorgb($hexvalue) {
    if ($hexvalue[0] == '#') {
        $hexvalue = substr($hexvalue, 1);
    }
    if (strlen($hexvalue) == 6) {
        list($r, $g, $b) = array($hexvalue[0] . $hexvalue[1], $hexvalue[2] . $hexvalue[3], $hexvalue[4] . $hexvalue[5]);
    } elseif (strlen($hexvalue) == 3) {
        list($r, $g, $b) = array($hexvalue[0] . $hexvalue[0], $hexvalue[1] . $hexvalue[1], $hexvalue[2] . $hexvalue[2]);
    } else {
        return false;
    }
    $r = hexdec($r);
    $g = hexdec($g);
    $b = hexdec($b);
    return array('R' => $r, 'G' => $g, 'B' => $b);
}

//
function Hex2RGB($color) {
    $color = str_replace('#', '', $color);
    if (strlen($color) != 6) {
        return array(0, 0, 0);
    }
    $rgb = array();
    for ($x = 0; $x < 3; $x++) {
        $rgb[$x] = hexdec(substr($color, (2 * $x), 2));
    }
    return $rgb;
}

//
function rgb2hex($rgb) {
    return sprintf("%06X", $rgb);
}

//
function color_inverse($color) {
    $color = str_replace('#', '', $color);
    if (strlen($color) != 6) {
        return '000000';
    }
    $rgb = '';
    for ($x = 0; $x < 3; $x++) {
        $c = 255 - hexdec(substr($color, (2 * $x), 2));
        $c = ($c < 0) ? 0 : dechex($c);
        $rgb .= (strlen($c) < 2) ? '0' . $c : $c;
    }
    return '#' . $rgb;
}

//
function ColorDarken($color, $dif = 20) {

    $color = str_replace('#', '', $color);
    if (strlen($color) != 6) {
        return '000000';
    }
    $rgb = '';

    for ($x = 0; $x < 3; $x++) {
        $c = hexdec(substr($color, (2 * $x), 2)) - $dif;
        $c = ($c < 0) ? 0 : dechex($c);
        $rgb .= (strlen($c) < 2) ? '0' . $c : $c;
    }

    return '#' . $rgb;
}

//
function random_color() {
    mt_srand((double) microtime() * 1000000);
    $c = '';
    while (strlen($c) < 6) {
        $c .= sprintf("%02X", mt_rand(0, 255));
    }
    return $c;
}


?>